<?php

namespace app\index\validate;

use think\Db;
use think\facade\Cookie;
use think\Validate;

class UserValidate extends Validate
{

    public $userData;

    protected $rule =   [
        '__token__' => 'token',
        'captcha|验证码'=>'require|captcha',
        'name|用户名'  => 'require|loginValiUsername',
        'password|密码'   => 'require|valiPassword'
    ];

    /***
     * 修改用户信息验证
     * @return UserValidate
     */
    public function sceneUpdateUser()
    {
        return $this->only(['__token__','password','rePassword'])
            ->remove('password', 'valiPassword');
    }

    /***
     * 注册验证
     * @return UserValidate
     */
    public function sceneRegister()
    {
        return $this->only(['__token__','name','password','rePassword'])
            ->remove('name', 'loginValiUsername')
            ->remove('password', 'valiPassword');
    }

    /***
     * 登录验证
     * @return UserValidate
     */
    public function sceneLogin()
    {
        if (Cookie::get("errorCount") < 3) {
            return $this->only(['name','password','__token__'])
                ->remove('name', 'registerValiUsername',"captcha");
        } else {
            return $this->only(['__token__','name','password','captcha'])
                ->remove('name', 'registerValiUsername');
        }
    }


    /**
     * 添加管理员验证
     * @return UserValidate
     */
    protected function sceneUserAdd()
    {
        return $this->only(['name'])
            ->remove(["__token__","password","captcha"]);
    }
    /**
     * 登录验证账号
     * @param $data
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function loginValiUsername($data)
    {
        $result = Db::table("user")->where("name",$data)->find();
        $this->userData = $data;
        if ($result !== null) {
            return true;
        } else {
            return "没有该用户";
        }
    }

    /***
     * 登录验证密码
     * @param $data
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function valiPassword($data)
    {
        $result = Db::table("user")->where("name",$this->userData)->find();
        $password = $data . $result["salt"];
        if (password_verify($password,$result["password"])) {
            $this->userData = $result;
            return true;
        } else {
            return "密码不正确";
        }
    }

    /***
     * 注册查看账号是否存在
     * @param $data
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function registerValiUsername($data)
    {
        $result = Db::table("admin")->where("name",$data)->find();
        if ($result !== null) {
            return "此用户已存在";
        } else {
            return true;
        }
    }
}